﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using KPIS.GERP.GIMS.BLL;
using KPIS.GERP.GIMS.IBLL;
using KPIS.GERP.GIMS.MODEL;
using System.Configuration;

namespace KPIS.GERP.GIMS.APP.APP_UI.Form
{
    public partial class Sys_File_View : System.Web.UI.Page
    {
        IBLLDocument bll = new DocumentBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string table_name = Request.QueryString["table"];
                string pk_field = Request.QueryString["field"];
                string seq = Request.QueryString["seq"];
                string DocPath = "";
                switch (table_name)
                {
                    case "ICM_PRE_PURCHASE_REQUEST_FILE" :
                        DocPath = @ConfigurationManager.AppSettings["PrePurchaseDocPath"].ToString();
                        break;
                    case "ICM_PURCHASE_REQUEST_FILE":
                        DocPath = @ConfigurationManager.AppSettings["PurchaseDocPath"].ToString();
                        break;
                    case "ICM_BIDDER_FILE":
                        DocPath = @ConfigurationManager.AppSettings["BidderDocPath"].ToString();
                        break;
                }
                //string FolderName = "Pre_Purchase_Request";
                string Extension = Request.QueryString["ex"];
                string Name = Request.QueryString["name"];
                string ContentType = Request.QueryString["Content_type"];

                //bll.DownloadDocument(table_name, pk_field, seq, Page, DocPath, FolderName);

                //string pathName = DocPath + FolderName + @"\" + seq + Extension;
                string pathName = DocPath + @"\" + seq + Extension;

                Response.ContentType = ContentType;

                Response.AddHeader("content-disposition", "attachment; filename=" + Name + Extension);

                FileStream sourceFile = new FileStream(pathName, FileMode.Open);

                long FileSize;

                FileSize = sourceFile.Length;

                byte[] getContent = new byte[(int)FileSize];

                sourceFile.Read(getContent, 0, (int)sourceFile.Length);

                sourceFile.Close();

                Response.BinaryWrite(getContent);

                Response.End();
            }
        }
    }
}
